package cn.imvc.app.models;

import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Notice;
import cn.imvc.app.repositories.NoticeRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class NoticeModelImpl implements NoticeModel{
	
	@Autowired
	private NoticeRepository noticeRepository;
	
	public Page<Notice> find(ViewRequest viewRequest) {
		return noticeRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}
	public Notice get(int id){
		return noticeRepository.findOne(id);
	}
	public Notice save(Notice notice) {
		notice.setCreated_at(new Timestamp(new Date().getTime()));
		notice.setUpdated_at(new Timestamp(new Date().getTime()));
		return noticeRepository.saveAndFlush(notice);
	}
	public Notice update(Notice newnotice) {
		Notice loadnotice=get(newnotice.getId());
		BeanUtils.copyProperties(newnotice, loadnotice, new String[]{"id","created_at","updated_at"});
		loadnotice.setUpdated_at(new Timestamp(new Date().getTime()));
		
		return noticeRepository.saveAndFlush(loadnotice);
	}
	public void delete(int id) {
		noticeRepository.delete(id);
		
	}
	public void deleteBatch(int[] ids) {

		for (int id : ids) {
			delete(id);
		}
		
	}

}
